﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using System.IO;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class AddTaskPlan : BRL.BaseEnterpriseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected Plan m_plan = null;
        protected R_EnterpriseCodification m_r_enterpriseCodification = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_plan = null;
            }
        }
        protected int FavoriteID
        {
            get { return this.GetVSInt("FavoriteID"); }
            set
            {
                this.SetVS("FavoriteID", value);
                m_r_enterpriseCodification = null;
            }
        }
        protected Plan Plan
        {
            get
            {
                if (m_plan == null)
                {
                    m_plan = new Plan();
                    if (LastID != 0)
                    {
                        m_plan.GetProperties(LastID);
                    }
                }
                return m_plan;
            }
        }
        protected R_EnterpriseCodification R_EnterpriseCodification
        {
            get
            {
                if (m_r_enterpriseCodification == null)
                {
                    m_r_enterpriseCodification = new R_EnterpriseCodification();
                    if (FavoriteID != 0)
                    {
                        m_r_enterpriseCodification.GetProperties(FavoriteID);
                    }
                }
                return m_r_enterpriseCodification;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            FavoriteID = this.GetInt(this.Request.QueryString["fid"]);
            if (R_EnterpriseCodification.ID == 0)
            {
                FavoriteID = 0;
            }
            LastID = this.GetInt(this.Request.QueryString["pid"]);
            if (Plan.ID == 0)
            {
                LastID = 0;
            }
            else
            {
                FavoriteID = Plan.FavoriteID;
            }
        }
        protected bool RB()
        {
            bool res = true;
            if (R_EnterpriseCodification.EnterpriseID != LoginUser.EnterpriseID)
            {
                    this.JSAlert("请选择合适的法律法规/标准来添加计划！");
                    this.JSCloseWindow();
                    res = false;
            }
            return res;
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = Plan.Name;
            this.txt_department.Text = Plan.Department;
            this.txt_expiredDT.Text = this.FormatDT(Plan.ExpiredDT);
            this.txt_person.Text = Plan.Person;
            this.txt_progress.Text = Plan.Progress;
            this.txt_comment.Text = Plan.Comment;
            this.chb_status.Checked = Plan.Status == 1;
            this.SetStatus();
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (Plan.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Plan.Name = this.txt_name.Text.Trim();
            Plan.Comment = this.txt_comment.Text;
            Plan.ChangeDT = DateTime.Now;
            Plan.Department = this.txt_department.Text;
            Plan.ExpiredDT = DateTime.Parse(this.txt_expiredDT.Text.Trim());
            Plan.Person = this.txt_person.Text;
            Plan.Progress = this.txt_progress.Text;
            Plan.Status = this.chb_status.Checked ? 1 : 0;
            Plan.FavoriteID = FavoriteID;
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            Plan.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            this.SetData();
            Plan.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }


}